/*
 * Copyright (c) 2006-2010, Petr Panteleyev
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, 
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products 
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.panteleyev.html.HTMLLinkDialog;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.ClipboardUtil;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SwingFactory;

/**
 *
 * @author Petr Panteleyev
 */
public class InsertReferenceDialog extends BasicDialog<JFrame> implements HTMLLinkDialog {
    private String url;
    private String refText;
    
    public InsertReferenceDialog(JFrame parent) {
        super(parent);
        this.url = ClipboardUtil.getAsUrl();
        initComponents();
    }
    
    public InsertReferenceDialog(JFrame parent, String url) {
        super(parent);
        this.url = url;
        initComponents();
    }

    public void setReferenceText(String text) {
        this.refText = text;        
    }
    
    public void setURL(String url) {
        this.url = url;
    }

    @Override
    public void dataToControls() {      
        this.referenceTextEdit.setText(this.refText);
        this.urlTextEdit.setText(this.url);
        this.linkTitleText.setText(null);
        
        this.boldCheckBox.setSelected(Options.getMakeLinkBold());
        this.italicCheckBox.setSelected(Options.getMakeLinkItalic());        
        
        pack();
    }

    @Override
    public String getReference() {
        StringBuilder b = new StringBuilder("<a href=\"");
        b.append(urlTextEdit.getText());
        b.append("\"");
                    
        String title = linkTitleText.getText();
        if (title.length() != 0) {
            b.append(" title='");
            b.append(title);
            b.append("'");
        }
                    
        if (newLinkWindowCheck.isSelected()) {
            b.append(" target='_blank'");
        }
                    
        b.append(">");
        
        if (boldCheckBox.isSelected()) {
            b.append("<b>");
        }
        if (italicCheckBox.isSelected()) {
            b.append("<i>");
        }
        b.append(referenceTextEdit.getText());                    
        if (italicCheckBox.isSelected()) {
            b.append("</i>");
        }
        if (boldCheckBox.isSelected()) {
            b.append("</b>");
        }
        b.append("</a>");
        return b.toString();        
    }    
    
    @Override
    protected void onOk() {
        Options.setMakeLinkBold(boldCheckBox.isSelected());
        Options.setMakeLinkItalic(italicCheckBox.isSelected());
        super.onOk();        
    }
            
    @Override
    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public void setInnerHTML(String html) {
        this.refText = html;   
    }

    @Override
    public String getUrl() {
        return urlTextEdit.getText();
    }

    @Override
    public String getInnerHTML() {
        return referenceTextEdit.getText();
    }

    @Override
    public boolean isBold() {
        return boldCheckBox.isSelected();
    }

    @Override
    public boolean isItalic() {
        return italicCheckBox.isSelected();
    }

    @Override
    public boolean isTargetBlank() {
        return newLinkWindowCheck.isSelected();
    }

    @Override
    public String getLinkTitle() {
        return linkTitleText.getText();
    }
    
    // UI Elements
    private JTextField referenceTextEdit;
    private JTextField linkTitleText;
    private JTextField urlTextEdit;
    private JCheckBox  boldCheckBox;
    private JCheckBox  italicCheckBox;
    private JCheckBox  newLinkWindowCheck;
    
    private void initComponents() {
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        setTitle(L10N.IRD_TITLE.s());
                
        JPanel mainPanel = SwingFactory.createPanel(
            BorderFactory.createEmptyBorder(10, 10, 10, 10), SwingFactory.LAYOUT.BOX_Y,
            new JLabel(L10N.IRD_LINK_TEXT_LABEL.s()),
            Box.createVerticalStrut(3),
            referenceTextEdit = new JTextField(40),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                boldCheckBox = new JCheckBox(L10N.IRD_BOLD_CHECK.s()),
                Box.createHorizontalStrut(3),
                italicCheckBox = new JCheckBox(L10N.IRD_ITALIC_CHECK.s()),
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            /* Title */
            new JLabel(L10N.IRD_LINK_TITLE_LABEL.s()),
            Box.createVerticalStrut(3),
            linkTitleText = new JTextField(40),
            Box.createVerticalStrut(5),
            /* URL */
            new JLabel("URL"),
            Box.createVerticalStrut(3),
            urlTextEdit = new JTextField(40),
            Box.createVerticalStrut(3),
            newLinkWindowCheck = new JCheckBox(L10N.IRD_NEW_WINDOW_CHECK.s())
        );

        boldCheckBox.setMaximumSize(boldCheckBox.getPreferredSize());
        SwingFactory.setAlignmentX(mainPanel, LEFT_ALIGNMENT);

        getContentPane().add(mainPanel);        
        getContentPane().add(new OkCancelButtonPanel(this, JComponent.LEFT_ALIGNMENT));        
        pack();
    }
    
}
